package com.gfxy.common.portal.service;

import com.gfxy.common.portal.dao.CommonMapper;
import com.gfxy.common.portal.dao.SysAppTreeMapper;
import com.gfxy.common.portal.entity.SysAppTree;
import com.gfxy.common.portal.entity.SysAppTreeExample;
import com.gfxy.commons.utils.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2019-05-14
 */
@Service
public class AppTreeService {

    private static final Logger logger = LoggerFactory.getLogger(AppTreeService.class);

    @Autowired
    private SysAppTreeMapper appTreeMapper;

    @Autowired
    private CommonMapper commonMapper;




    public List<SysAppTree> getTopAppTree(){
        SysAppTreeExample example = new SysAppTreeExample();
        example.createCriteria().andParentIdEqualTo("-1");
        List<SysAppTree> result = appTreeMapper.selectByExample(example);
        return result;
    }

    public List<SysAppTree> getAppTreeByParentId(String parentId){
        SysAppTreeExample example = new SysAppTreeExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<SysAppTree> result = appTreeMapper.selectByExample(example);
        return result;
    }

    public SysAppTree save(SysAppTree appTree){

        if(appTree.getId()==null || appTree.getId().isEmpty()){
            appTree.setId(UUIDUtil.getUUID());
            appTreeMapper.insert(appTree);
            return appTree;
        } else {
            appTreeMapper.updateByPrimaryKey(appTree);
            return appTree;
        }
    }


    public List<SysAppTree> getAppTreeByParentIdForMenu(String menuParentId,String userName) {
        logger.info("开始查询 menuParentId 为：[-1] 的菜单树,userName 为：[" + userName + "]的顶部菜单");
        if(userName.equals("admin")){
            logger.debug("user is admin ");
            if(menuParentId.equals("-1")){

                SysAppTreeExample example = new SysAppTreeExample();
                example.createCriteria().andParentIdEqualTo("-1");
                List<SysAppTree> result = appTreeMapper.selectByExample(example);
//                List<SysAppTree> result = appTreeMapper.getAppsByAdmin(menuParentId);
                return result;
            } else {
                List<SysAppTree> result = appTreeMapper.getAppTreeByParentIdForMenu(menuParentId);
                return result;
            }

        } else {
            if(menuParentId.equals("-1")){
                List<SysAppTree> result = appTreeMapper.getTopAppsByUser(userName);
                return result;
            } else {
                List<SysAppTree> result = appTreeMapper.getAppTreeByParentIdAndUserForMenu(menuParentId,userName);
                return result;
            }

        }

    }


    public List<Map> getRoleMenuType(String roleId, String systemIds){
        String[] systems = systemIds.split(",");
        String newSystemIds = "";
        for (String system : systems) {
            newSystemIds = newSystemIds + ",'"+system +"'";
        }

        List<Map>  lists = commonMapper.selectDataByRoleId(roleId,newSystemIds);
        return lists;
    }




}
